package com.lagou.service;

import com.lagou.dao.LagouTokenDao;
import com.lagou.dao.UserDao;
import com.lagou.pojo.LagouToken;
import com.lagou.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    public String insertUser(String email, String password){
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        int result = userDao.insertUser(user);
        //存储token
        UUID uuid = UUID.randomUUID();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(uuid.toString());
        lagouTokenDao.insertToken(lagouToken);
        return uuid.toString();
    }

    public User selectUser(String email){
        User user = userDao.selectUser(email);
        return user;
    }

    public String validate(String email, String password){
        User user = userDao.validate(email, password);
        if(user != null){
            UUID uuid = UUID.randomUUID();
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(uuid.toString());
            lagouTokenDao.insertToken(lagouToken);
            return uuid.toString();
        }
        return "";
    }
}
